package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class UDPServer {
	public byte[] getBytesFromFile(File file) throws IOException {
		InputStream is = new FileInputStream(file);

		// Get the size of the file
		long length = file.length();

		// You cannot create an array using a long type.
		// It needs to be an int type.
		// Before converting to an int type, check
		// to ensure that file is not larger than Integer.MAX_VALUE.
		if (length > Integer.MAX_VALUE) {
			// File is too large
		}

		// Create the byte array to hold the data
		byte[] bytes = new byte[(int) length];

		// Read in the bytes
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}

		// Ensure all the bytes have been read in
		if (offset < bytes.length) {
			throw new IOException("Could not completely read file "
					+ file.getName());
		}

		// Close the input stream and return bytes
		is.close();
		return bytes;
	}

	public boolean data_sender(int SERVER_PORT) {
		try {
			DatagramSocket serverSocket = new DatagramSocket(SERVER_PORT);
			while (true) {

				byte[] receiveData = new byte[1024];
				System.out.println("Waiting for Filename");

				DatagramPacket receivePacket = new DatagramPacket(receiveData,
						receiveData.length);
				serverSocket.receive(receivePacket);
				String fileName = new String(receivePacket.getData());
				System.out.println("Received request for " + fileName);
				// Read the file and send it
				File file = new File(fileName);
				// Get the size of the file
				long length = file.length();
				byte[] sendData = new byte[24000];
				// /////////////////////////////////
				InputStream is = new FileInputStream(file);

				// You cannot create an array using a long type.
				// It needs to be an int type.
				// Before converting to an int type, check
				// to ensure that file is not larger than Integer.MAX_VALUE.
				// if (length > Integer.MAX_VALUE) {
				// System.err.println("File too large");
				// }

				// Create the byte array to hold the data
				byte[] bytes = new byte[24000];

				// Read in the bytes
				int offset = 0;
				int numRead = 0;
				int count=0;
				while (offset < length
						&& (numRead = is.read(bytes, 0, 24000)) >= 0) {
					offset += numRead;
					System.out.println("Sent: " + offset+" bytes");
					sendData = bytes;
					bytes = new byte[24000];
					InetAddress IPAddress = receivePacket.getAddress();
					int port = receivePacket.getPort();

					DatagramPacket sendPacket = new DatagramPacket(sendData,
							sendData.length, IPAddress, port);
					serverSocket.send(sendPacket);
					count++;
					Thread.currentThread().sleep(30);
				}

				// Ensure all the bytes have been read in
				if (offset < length) {
					throw new IOException("Could not completely read file "
							+ file.getName());
				}

				// Close the input stream and return bytes
				is.close();
				System.out.println("Sent "+count+" packets");
				System.out
						.println("Sent the Contents...Waiting for more requests");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}